
#include "HMMBuilder.h"
#include <fstream>

#include "iostream"

typedef std::vector<unsigned> Observation;

void HMMBuilder::build(const Track &track, HMM &hmm) {
	Observation obs;
	track.convert(obs, hmm.getAlphabetSize());
	hmm.train(obs);
}

bool HMMBuilder::build(const char * fileName, HMM &hmm) {
	ifstream fin;
	fin.open(fileName);
	if (fin) {
		Track track;
		std::vector<Observation> obs;
		while (!fin.eof()) {
			unsigned n = 0;
			int x;
			int y;
			fin >> n;
			//std::cout << "buildHMM. addTrack: n = " << n << "\n";
			track.clear();
			for (unsigned i = 0; i < n; i++) {
				fin >> x >> y;
				track.addPoint(x, y);
			}
			if (n >= 3) {
				obs.push_back(Observation());
				track.convert(obs.back(), hmm.getAlphabetSize());
			}
		}
		fin.close();
		//std::cout << "buildHMM: obs.size() = " << obs.size() << "\n";
		std::cout << "\nbuildHMM: observations\n";
		for (size_t i = 0; i < obs.size(); i++) {
			for (size_t j = 0; j < obs[i].size(); j++) {
				std::cout << obs[i][j] << " ";
			}
			std::cout << "\n\n";
		}
		std::cout << "\n";
		hmm.train(obs);
		return true;
	}
	return false;
}

